package MyBitSet;

import java.util.Arrays;

/**
 * Description:
 * User:岁亮亮
 * Date:2025-03-19
 * TIME:10:25
 */
public class MyBiterSet {

    public byte[] elementData;
    public static final int initCapacity = 1;
    public int usedSize = 0;

    public MyBiterSet(){
        this.elementData = new byte[initCapacity];
    }
    public MyBiterSet(int capacity){
        this.elementData = new byte[capacity/8 + 1];
    }
    public int getUsedSize(){
        return this.usedSize;
    }
    public void set(int target){
        if(target < 0){
            throw new IllegalArgumentException();
        }
        //检查是否扩容;
        int arrayIndex = target / 8;
        if(arrayIndex >= this.elementData.length){
            //扩容
            expansion(arrayIndex);
        }
        int bitIndex = target % 8;
        this.elementData[arrayIndex] |= (1 << bitIndex);
        usedSize++;
    }
    public boolean get(int target){
        if(target < 0){
            throw new IllegalArgumentException();
        }
        int arrayIndex = target / 8;
        int bitIndex = target % 8;
        return ((this.elementData[arrayIndex] & (1 << bitIndex)) != 0);
    }
    public void reSet(int target){
        if(target < 0){
            throw new IllegalArgumentException();
        }
        int arrayIndex = target / 8;
        int bitIndex = target % 8;
        this.elementData[arrayIndex] &= ~(1 << bitIndex);
        usedSize--;
    }
    public void sort(){
        int len = this.elementData.length;
        for(int i = 0; i < len; i++){
            for(int j = 0; j < 8; j++){
                if(((this.elementData[i] & (1 << j)) != 0)){
                    System.out.println(i*8 + j);
                }
            }
        }
    }

    private void expansion(int arrayIndex) {
        this.elementData = Arrays.copyOf(this.elementData, arrayIndex + 1);
    }


}
